﻿
#Region "PL"

Partial Class Public_ManageDownload_SearchDownload
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindData()
        End If
    End Sub

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If clsAuth.GetUserID() <> "" Then
            Me.Master.MasterPageFile = "~\WebForm\MasterPage.master"
        End If
    End Sub

    '下載檔案 
    Protected Sub lbtnDownLoadFile_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim fileName As String = Me.gv.DataKeys(DirectCast(DirectCast(sender, LinkButton).NamingContainer, GridViewRow).RowIndex).Value.ToString()
        Me.DownloadFile(fileName)
    End Sub
End Class

#End Region

#Region "BLL"
Partial Class Public_ManageDownload_SearchDownload
    Private Sub BindData()
        Dim dt As Data.DataTable = Me.GetData()
        dt.DefaultView.Sort = Me.gv.SortSQL
        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub

    Private Function DownloadFile(ByVal fileName As String) As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("SELECT * ")
        sql.AppendFormat("FROM UploadFiles ")
        sql.AppendFormat("WHERE FileSaveName='{0}' ", fileName)

        Dim tmpTable As Data.DataTable = dat.GetData(sql)

        If tmpTable IsNot Nothing AndAlso tmpTable.Rows.Count > 0 Then

            Dim objDataRow As Data.DataRow = tmpTable.Rows(0)

            Dim FileInfo As New System.IO.FileInfo((Server.MapPath(objDataRow("FileSavePath").ToString()) + objDataRow("FileSaveName").ToString() & ".") + objDataRow("FileType").ToString())

            If FileInfo.Exists Then
                Me.Response.Clear()
                Me.Response.AddHeader("Content-Disposition", ("attachment;filename=" & objDataRow("FileName")))
                Me.Response.HeaderEncoding = System.Text.Encoding.GetEncoding("big5")
                Me.Response.AddHeader("Content-Length", FileInfo.Length.ToString())
                Me.Response.ContentType = "application/octet-stream"
                Me.Response.WriteFile(FileInfo.FullName)

                Me.Response.End()

                Return True
            End If

        End If

        MyObj.ShowMessage("檔案不存在")
    End Function

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.BindData()
    End Sub

    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindData()
    End Sub
End Class
#End Region

#Region "DAL"
Partial Class Public_ManageDownload_SearchDownload
    Private Function GetData() As Data.DataTable
        Dim dal As New BLL_Sys_UpLoadFiles
        Dim userID As String = clsAuth.GetUserID()

        Return dal.BLL_SelectAuth("PublicDownload", "", userID)
    End Function
End Class
#End Region
